﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WinnerCMS.Model;
using System.Data.Entity;
using WinnerCMS.Common;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 角色
    /// </summary>
    public class RoleDal
    {
        #region 添加角色
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <param name="rightIds"></param>
        /// <returns></returns>
        /// <remarks>多对多录入的典范</remarks>
        public int Add(Role role, string rightIds)
        {
            using (Entities db = new Entities())
            {
                var ids = rightIds.Split(',');
                foreach (var right in db.Rights.Where(x => ids.Contains(x.Id)))
                {
                    role.Rights.Add(right);
                }
                db.Roles.Add(role);

                db.SaveChangesAsync().Wait();

                return role.Id;
            }
        }

        #endregion

        #region 修改角色
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="role"></param>
        /// <param name="rightIds"></param>
        /// <remarks>多对多修改的典范</remarks>
        public void Edit(Role role, string rightIds)
        {
            using (Entities db = new Entities())
            {
                string[] ids = string.IsNullOrEmpty(rightIds) ? new string[] { } : rightIds.Split(',');

                //上下文再查询一遍原始数据，然后用传递进来的实体类赋值。
                var _role = db.Roles.Include(x => x.Rights).First(x => x.Id == role.Id);
                _role.RoleName = role.RoleName;
                _role.Remark = role.Remark;


                var before = _role.Rights.Select(x => x.Id);
                //删掉已经不要的
                before.Except(ids).ToList().ForEach(x => _role.Rights.Remove(db.Rights.Find(x)));
                //加入新增加的
                ids.Except(before).ToList().ForEach(x => _role.Rights.Add(db.Rights.Find(x)));

                db.Entry(_role).State = EntityState.Modified;


                db.SaveChangesAsync().Wait();

            }
        }

        #endregion



        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        public void Delete(int roleId)
        {
            using (Entities db = new Entities())
            {
                db.Roles.Remove(db.Roles.FirstOrDefault(x => x.Id == roleId));


                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 加载角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Role GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.Roles.Include(x => x.Rights).FirstOrDefault(x => x.Id == id);
            }
        }

        /// <summary>
        /// 显示当前用户没有的角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public IList<Role> GetListNotIn(int userId, bool isAdmin)
        {
            using (Entities db = new Entities())
            {
                var q = from u in db.Users
                        from r in db.Roles
                        where u.Id == userId && u.Roles.Contains(r) == false && r.IsAdmin == isAdmin
                        select r;

                return q.ToList();
            }
        }

        /// <summary>
        /// 显示当前用户含有的角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public IList<Role> GetListIn(int userId, bool isAdmin)
        {
            using (Entities db = new Entities())
            {
                var q = from u in db.Users
                        from r in db.Roles
                        where u.Id == userId && u.Roles.Contains(r) && r.IsAdmin == isAdmin
                        select r;

                return q.ToList();
            }
        }
        /// <summary>
        /// 取消某个用户的某个角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleId"></param>
        public void TakeIt(int userId, int roleId)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.LazyLoadingEnabled = true;
                var user = db.Users.First(x => x.Id == userId);
                user.Roles.Remove(db.Roles.Find(roleId));
                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 授予某个用户的某个角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleId"></param>
        public void AuthorizeIt(int userId, int roleId)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.LazyLoadingEnabled = true;
                var user = db.Users.First(x => x.Id == userId);
                user.Roles.Add(db.Roles.Find(roleId));
                db.SaveChangesAsync().Wait();
            }
        }

        /// <summary>
        /// 打开角色管理的时候加载角色
        /// </summary>
        /// <returns></returns>
        public IList<Role> BindList(bool isAdmin)
        {
            using (Entities db = new Entities())
            {
                return db.Roles.Where(x => x.IsAdmin == isAdmin).ToList();
            }
        }
    }
}